import requests
from lxml import html
# from urlparse import urljoin


def meta_redir_url(r):
    # mime = magic.from_buffer(r.content, mime=True)
    # extension = mimetypes.guess_extension(mime)
    # if extension == '.html':
    try:
        html_tree = html.fromstring(r.text)
        attr = html_tree.xpath(
            "//meta[translate(@http-equiv, 'REFSH', 'refsh') = 'refresh']/@content")[0]
        wait, text = attr.split(";")
        if text.lower().startswith("url="):
            url = text[4:]
            # if not url.startswith('http'):
            #     # Relative URL, adapt
            #     url = urljoin(r.url, url)
            return url
    except:
        # print("No meta redir")
        return None
    return None
